home *** CD-ROM | disk | FTP | other *** search
- //****************************************************************************
- // File: glfont.c
- //
- // Purpose: Contains main message loop and application entry point. Also contains
- // the code to handle palettes properly in an OpenGL application and the
- // main window callback procedure.
- //
- // Functions:
- // WinMain - initializes everything and enters message loop
- // InitializeWindow - Initializes the window class of and creates the main window
- // MainWndProc - Main window procedure
- // CommandHandler - Handle all WM_COMMAND messages
- // ChangeFont - Allows the user to change the current TrueType font displayed.
- //
- // Development Team:
- // Greg Binkerd - Windows Developer Support
- //
- // Written by Microsoft Windows Developer Support
- // Copyright (c) 1995 Microsoft Corporation. All rights reserved.
- //****************************************************************************
-
- #include "glfont.h"
- #include "resource.h"
-
- CHAR szAppName[]="GLFont";
-
- HGLRC hRC;
-
- HWND ghWnd;
- BOOL bDraw = TRUE;
-
- extern HPALETTE ghpalOld, ghPalette;
-
- /* forward declarations of helper functions in this module */
- HWND WINAPI InitializeWindow (HINSTANCE, int);
- LONG WINAPI CommandHandler (HWND, WPARAM, LPARAM);
- LONG WINAPI MainWndProc (HWND, UINT, WPARAM, LPARAM);
- void ChangeFont(HWND hWnd);
-
-
- //***********************************************************************
- // Function: WinMain
- //
- // Purpose: Called by Windows on app startup. Initializes everything,
- // and enters a message loop.
- //
- // Parameters:
- // hInstance == Handle to _this_ instance.
- // hPrevInstance == Handle to last instance of app.
- // lpCmdLine == Command Line passed into app.
- // nCmdShow == How app should come up (i.e. minimized/normal)
- //
- // Returns: Return value from PostQuitMessage.
- //
- // Comments:
- //
- // History: Date Author Reason
- // 6/08/94 Created
- //****************************************************************************
-
- int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- MSG msg;
- HWND hWnd;
-
- /* previous instances do not exist in Win32 */
- if (hPrevInstance)
- return 0;
-
- if (!(hWnd = InitializeWindow (hInstance, nCmdShow)))
- return FALSE;
-
- /* main window message loop */
- while (GetMessage (&msg, NULL, 0, 0))
- {
- TranslateMessage (&msg);
- DispatchMessage (&msg);
- }
-
- /* return success of application */
- return TRUE;
- }
-
-
- //****************************************************************************
- // Function: InitializeWindow
- //
- // Purpose: Called by WinMain on first instance of app. Registers
- // the window class.
- //
- // Parameters:
- // hInstance == Handle to _this_ instance.
- // nCmdShow == Initial state of window
- //
- // Returns: Handle to newly created window if successful
- //
- // Comments:
- //
- // History: Date Author Reason
- // 5/30/95 Modified
- //****************************************************************************
-
- HWND WINAPI InitializeWindow (HINSTANCE hInstance, int nCmdShow)
- {
- WNDCLASS wc;
- HWND hWnd;
-
- /* Register the frame class */
- wc.style = 0;
- wc.lpfnWndProc = (WNDPROC)MainWndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon (hInstance, szAppName);
- wc.hCursor = LoadCursor (NULL,IDC_ARROW);
- wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
- wc.lpszMenuName = szAppName;
- wc.lpszClassName = szAppName;
-
- if (!RegisterClass (&wc) )
- return FALSE;
-
- /* Create the frame */
- ghWnd = hWnd = CreateWindow (szAppName,
- "wglUseFontOutlines OpenGL Sample",
- WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- NULL,
- NULL,
- hInstance,
- NULL);
-
- /* make sure window was created */
- if (!hWnd)
- return FALSE;
-
- /* show and update main window */
- ShowWindow (hWnd, nCmdShow);
-
- UpdateWindow (hWnd);
-
- return hWnd;
- }
-
-
- //****************************************************************************
- // Function: MainWndProc
- //
- // Purpose: Message handler for main overlapped window.
- //
- // Parameters:
- // hWnd == Handle to this window.
- // uMsg == Message to process.
- // wParam == WORD parameter -- depends on message
- // lParam == LONG parameter -- depends on message
- //
- // Returns: Depends on message.
- //
- // Comments:
- //
- // History: Date Author Reason
- // 3/30/95 GGB Modified
- //****************************************************************************
-
- LONG WINAPI MainWndProc (
- HWND hWnd,
- UINT uMsg,
- WPARAM wParam,
- LPARAM lParam)
- {
- LONG lRet = 1;
- UINT nTimer;
-
- switch (uMsg)
- {
- case WM_CREATE:
- {
- HDC hDC;
- LOGFONT lf;
- HFONT hFont, hOldFont;
- GLYPHMETRICSFLOAT agmf[256];
-
- hDC = GetDC(hWnd);
- bSetupPixelFormat(hDC);
-
- hRC = wglCreateContext( hDC );
- wglMakeCurrent( hDC, hRC );
-
- // Let's create a default TrueType font to display.
- memset(&lf,0,sizeof(LOGFONT));
- lf.lfHeight = -28 ;
- lf.lfWeight = FW_NORMAL ;
- lf.lfCharSet = ANSI_CHARSET ;
- lf.lfOutPrecision = OUT_DEFAULT_PRECIS ;
- lf.lfClipPrecision = CLIP_DEFAULT_PRECIS ;
- lf.lfQuality = DEFAULT_QUALITY ;
- lf.lfPitchAndFamily = FF_DONTCARE|DEFAULT_PITCH;
- lstrcpy (lf.lfFaceName, "Arial") ;
-
- hFont = CreateFontIndirect(&lf);
- hOldFont = SelectObject(hDC,hFont);
-
- // Create a set of display lists based on the glyphs of the TT font we selected
- if (!(wglUseFontOutlines(hDC, 0, 255, GLF_START_LIST, 0.0f, 0.15f,
- WGL_FONT_POLYGONS, agmf)))
- MessageBox(hWnd,"wglUseFontOutlines failed!","GLFont",MB_OK);
-
- DeleteObject(SelectObject(hDC,hOldFont));
-
- // Set timer to animate the text
- nTimer = SetTimer(hWnd, 0, 50, NULL);
- break;
- }
-
- case WM_TIMER:
- // If ready and not iconized, draw the text.
- if ((bDraw) && (!(IsIconic(hWnd))))
- draw_scene(hWnd);
- break;
-
- case WM_SIZE:
- resize(hWnd);
- DefWindowProc (hWnd, uMsg, wParam, lParam);
- break;
-
- // The WM_QUERYNEWPALETTE message informs a window that it is about to
- // receive input focus. In response, the window receiving focus should
- // realize its palette as a foreground palette and update its client
- // area. If the window realizes its palette, it should return TRUE;
- // otherwise, it should return FALSE.
-
- case WM_QUERYNEWPALETTE:
- {
- HDC hDC;
-
- if(ghPalette)
- {
- hDC = GetDC(hWnd);
-
- // Select and realize the palette
-
- ghpalOld = SelectPalette(hDC, ghPalette, FALSE);
- RealizePalette(hDC);
-
- // Redraw the client area
-
- InvalidateRect(hWnd, NULL, TRUE);
- UpdateWindow(hWnd);
-
- if(ghpalOld)
- SelectPalette(hDC, ghpalOld, FALSE);
-
- ReleaseDC(hWnd, hDC);
-
- return TRUE;
- }
-
- return FALSE;
- }
-
- // The WM_PALETTECHANGED message informs all windows that the window
- // with input focus has realized its logical palette, thereby changing
- // the system palette. This message allows a window without input focus
- // that uses a color palette to realize its logical palettes and update
- // its client area.
- //
- // This message is sent to all windows, including the one that changed
- // the system palette and caused this message to be sent. The wParam of
- // this message contains the handle of the window that caused the system
- // palette to change. To avoid an infinite loop, care must be taken to
- // check that the wParam of this message does not match the window's
- // handle.
-
- case WM_PALETTECHANGED:
- {
- HDC hDC;
-
- // Before processing this message, make sure we
- // are indeed using a palette
-
- if (ghPalette)
- {
- // If this application did not change the palette, select
- // and realize this application's palette
-
- if (wParam != (WPARAM)hWnd)
- {
- // Need the window's DC for SelectPalette/RealizePalette
-
- hDC = GetDC(hWnd);
-
- // Select and realize our palette
-
- ghpalOld = SelectPalette(hDC, ghPalette, FALSE);
- RealizePalette(hDC);
-
- // WHen updating the colors for an inactive window,
- // UpdateColors can be called because it is faster than
- // redrawing the client area (even though the results are
- // not as good)
-
- UpdateColors(hDC);
-
- // Clean up
-
- if (ghpalOld)
- SelectPalette(hDC, ghpalOld, FALSE);
-
- ReleaseDC(hWnd, hDC);
- }
- }
- break;
- }
-
- case WM_COMMAND:
- lRet = CommandHandler (hWnd, wParam, lParam);
- break;
-
- case WM_CLOSE:
- {
- /* call destroy window to cleanup and go away */
- DestroyWindow (hWnd);
- }
- break;
-
- case WM_DESTROY:
- {
- HGLRC hRC;
- HDC hDC;
-
- // Clean up timer and display lists
- KillTimer(hWnd,nTimer);
- glDeleteLists(GLF_START_LIST, 256);
-
- /* release and free the device context and rendering context */
- hRC = wglGetCurrentContext();
- hDC = wglGetCurrentDC();
-
- wglMakeCurrent(NULL, NULL);
-
- if (hRC)
- wglDeleteContext(hRC);
- if (hDC)
- ReleaseDC(hWnd, hDC);
- PostQuitMessage (0);
- }
- break;
-
- default:
- /* pass all unhandled messages to DefWindowProc */
- lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
- break;
- }
-
- /* return 1 if handled message, 0 if not */
- return lRet;
- }
-
-
- //****************************************************************************
- // Function: CommandHandler
- //
- // Purpose: Command handler for main overlapped window.
- //
- // Parameters:
- // hWnd == Handle to this window.
- // wParam == WORD parameter -- depends on message
- // lParam == LONG parameter -- depends on message
- //
- // Returns: Depends on message.
- //
- // Comments:
- //
- // History: Date Author Reason
- // 3/15/95 Created
- //****************************************************************************
-
- LONG WINAPI CommandHandler (
- HWND hWnd,
- WPARAM wParam,
- LPARAM lParam)
- {
- switch (LOWORD(wParam))
- {
- case IDM_EXIT:
- /* exit application */
- PostMessage (hWnd, WM_CLOSE, 0, 0L);
- break;
-
- case ID_CHANGEFONT:
- ChangeFont(hWnd);
- break;
-
- default:
- return FALSE;
- }
- return TRUE;
- }
-
-
- //****************************************************************************
- // Function: ChangeFont
- //
- // Purpose: Allows the user to change the TrueType font that is displayed
- // in the main window. If the user selects a new font, this function
- // will delete the display lists for the old font and create a new
- // set of display lists for the new font with wglUseFontOutlines
- //
- // Parameters:
- // hWnd == Handle to this window.
- //
- // Returns: void
- //
- // Comments:
- //
- // History: Date Author Reason
- // 5/31/95 GGB Created
- //****************************************************************************
-
- void ChangeFont(HWND hWnd)
- {
- CHOOSEFONT chf;
- HDC hDC;
- LOGFONT lf;
- HFONT hFont, hOldFont;
- GLYPHMETRICSFLOAT agmf[256];
-
-
- // Don't draw while we are doing this.
- bDraw = FALSE;
- // Erase what is currently on the window
- InvalidateRect(hWnd,NULL,TRUE);
- // Fill in the CHOOSEFONT structure
- memset(&chf,0,sizeof(CHOOSEFONT));
- chf.lStructSize = sizeof(CHOOSEFONT);
- chf.hwndOwner = hWnd;
- chf.lpLogFont = &lf;
- chf.Flags = CF_SCREENFONTS | CF_EFFECTS | CF_INITTOLOGFONTSTRUCT | CF_TTONLY;
- chf.nFontType = SCREEN_FONTTYPE;
-
- if (ChooseFont( &chf ))
- {
- // Delete the current font's display lists
- glDeleteLists(GLF_START_LIST, 256);
- hDC = wglGetCurrentDC();
- hFont = CreateFontIndirect(&lf);
- hOldFont = SelectObject(hDC,hFont);
-
- // Create a set of display lists based on the glyphs of the TT font selected
- if (!(wglUseFontOutlines(hDC, 0, 255, GLF_START_LIST, 0.0f, 0.15f,
- WGL_FONT_POLYGONS, agmf)))
- MessageBox(hWnd,"wglUseFontOutlines failed!","GLFont",MB_OK);
- DeleteObject(SelectObject(hDC,hOldFont));
- InvalidateRect(hWnd,NULL,TRUE);
- }
- bDraw = TRUE;
- }
-